#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Author: Faker.Deng
Exercise after Chapter 1 Problem 10
"""
from problem9 import *


class Library(object):
    """Library class"""
    def __init__(self, name):
        self.__name = name
        self.__book = []
        self.__patron = []

    def __str__(self):
        return self.__name

    def reset(self):
        self.__book = []
        self.__patron = []

    def add_patron(self, *class_patron):
        for patron in class_patron:
            self.__patron.append(patron)

    def del_patron(self, *class_patron):
        for patron in class_patron:
            self.__patron.remove(patron)

    def add_book(self, *class_book):
        for book in class_book:
            self.__book.append(book)

    def del_book(self, *class_book):
        for book in class_book:
            self.__book.remove(book)

    def find_book(self, book_name):
        for book in self.__book:
            if book.get_name() == book_name:
                return book
        print("Can't find book <" + book_name + ">.")

    def find_patron(self, patron_name):
        for patron in self.__patron:
            if patron.get_name() == patron_name:
                return patron
        print("Can't find patron \"" + patron_name + "\".")

    def get_book_list(self):
        print("List of books:")
        for book in self.__book:
            print(book)

    def get_patron_list(self):
        print("List of patrons:")
        for patron in self.__patron:
            print(patron)

    def get_book_info(self, book_name):
        book = self.find_book(book_name)
        book.info()

    def get_patron_info(self, patron_name):
        patron = self.find_patron(patron_name)
        patron.info()

    def borrow_book(self, patron_name, *book_name_list):
        patron = self.find_patron(patron_name)
        if patron is None:
            return
        for book_name in book_name_list:
            book = self.find_book(book_name)
            if book is None:
                return
            book.add_reader(patron_name)
            patron.borrow_book(book_name)

    def bespeak_book(self, patron_name, *book_name_list):
        for book_name in book_name_list:
            book = self.find_book(book_name)
            if book is None:
                return
            book.add_bespeak(patron_name)

    def return_book(self, patron_name, *book_name_list):
        patron = self.find_patron(patron_name)
        if patron is None:
            return
        for book_name in book_name_list:
            book = self.find_book(book_name)
            if book is None:
                return
            book.return_book()
            patron.return_book(book_name)

    def info(self):
        self.get_patron_list()
        self.get_book_list()


# Test book class
def test_library_class():
    book1 = Book(name="Harry Potter and the Philosopher's Stone", author="J. K. Rowling")
    book2 = Book(name="Harry Potter and the Chamber of Secrets", author="J. K. Rowling")
    patron1 = Patron(name="ZhangSan")
    patron2 = Patron(name="LiSi")
    library = Library(name="Library")
    library.add_book(book1, book2)
    library.add_patron(patron1, patron2)
    library.get_book_list()
    library.get_patron_list()

    library.borrow_book("ZhangSan", "Harry Potter and the Philosopher's Stone")
    library.get_book_info("Harry Potter and the Philosopher's Stone")
    library.get_patron_info("ZhangSan")
    library.borrow_book("LiSi", "Harry Potter and the Philosopher's Stone")
    library.bespeak_book("LiSi", "Harry Potter and the Philosopher's Stone")
    library.get_book_info("Harry Potter and the Philosopher's Stone")
    library.return_book("ZhangSan", "Harry Potter and the Philosopher's Stone")
    library.get_book_info("Harry Potter and the Philosopher's Stone")
    library.get_patron_info("ZhangSan")

    library.info()


if __name__ == "__main__":
    test_library_class()
